/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bbddmovil;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.query.Predicate;
import java.util.List;

/**
 *
 * @author Ismael gros vidal
 */
public class BBDDmovil {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        
        EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
        config.common().objectClass(Companyia.class).cascadeOnDelete(true);
        ObjectContainer db = Db4oEmbedded.openFile(config, "DB4OFILENAME");
        
        try {
            
            
                      
           
                    //INSERIR 4 TELEFONS MOVILS
            
                      
                      
                        Companyia com1 = new Companyia("Movistar");
			Movil mov1 = new Movil("Samsung","S2");
                        com1.setMovil(mov1);
                        db.store(com1);
                        System.out.println("Movil " + mov1 +"   Companyia " + com1 );
                         
			 
                        Companyia com2 = new Companyia("Orange");
			Movil mov2 = new Movil("Iphone", "4S");
			com2.setMovil(mov2);
                        db.store(com2);
			 System.out.println("Movil " + mov2 +"   Companyia " + com2 );
                        
                        Companyia com3 = new Companyia("Vodafone");
                        Movil mov3 = new Movil("LG", "L9");
			com3.setMovil(mov3);
                        db.store(com3);
			 System.out.println("Movil " + mov3 +"   Companyia " + com3 );
                        
                        
                        Companyia com4 = new Companyia("Amena");
                        Movil mov4 = new Movil("Samsung", "Ace");
			com4.setMovil(mov4);
                        db.store(com4);
			System.out.println("Movil " + mov4 +"   Companyia " + com4 );
                      
                      
            
            
            // Consultar tots els models de mobils que hi ha en una BDOO
            
                      Companyia proto = new Companyia( null);
			ObjectSet<List<?>> result = db.queryByExample(proto);
			listResult(result);
              
            //Consultar un determinat companyia de mobil
            
           
               
                  List<Companyia> result2 = db.query(new Predicate<Companyia>() {
                  public boolean match(Companyia companyia) {
                    return companyia.getCompanyia().equals("Vodafone");
                    }
                    });
                    listResult(result2);   
                
                      
            
            //4. Eliminar una determinada companyia de mobil
          
        List<Companyia> result1 = db.query(new Predicate<Companyia>() {
                        @Override
        public boolean match(Companyia companyia) {
        return companyia.getCompanyia().equals("Orange");

        }
        });
     
    
        if (result.size() > 0) {
        Companyia found = result1.get(0);
        db.delete(found);
        }
        result1 = db.query(new Predicate<Companyia>() {
                        @Override
        public boolean match(Companyia companyia) {
        return true;
        }
        });
        listResult(result1);
            
          
                      
		} finally {
		    db.close();
		}
                
	}
	
	public static void listResult(List<?> result){
	    System.out.println(result.size());
	    for (Object o : result) {
	      System.out.println(o);   	
	    }
            
            
	 }

        }
    

